package com.jy.appmarket.activity;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jy.app.market.idata.data.Login;
import com.jy.appmarket.AppMarketApplication;
import com.jy.appmarket.R;
import com.jy.appmarket.utils.LoginUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;

public class LogoutActivity extends Activity implements OnClickListener{
	private static final int REQUEST_MODIFY_USER_INFO_CODE = 12;
	private static final int REQUEST_BIND_PHONE_INFO_CODE = 13;
	
	private ImageView mHeadIconImg;
	private TextView mNickNameTv;
	private TextView mPhomeNumTv;
	private TextView mBindHintTv;
	private RelativeLayout mBindPhoneLayout;
	private RelativeLayout mModifyPwdLayout;
	private View mModifyDivider;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_logout);
		
		initTitle();
		initViews();
	}
	
	private void initTitle() {
		ActionBar actionBar = getActionBar();
		actionBar.setTitle("个人资料");
		actionBar.setDisplayUseLogoEnabled(false);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayHomeAsUpEnabled(true);
	}

	private void initViews() {
		mHeadIconImg = findViewById(R.id.head_icon);
		mNickNameTv = findViewById(R.id.nick_name);
		mPhomeNumTv = findViewById(R.id.phone_num_text);
		mBindHintTv = findViewById(R.id.bind_hint);
		
		findViewById(R.id.head_layout).setOnClickListener(this);
		findViewById(R.id.logout_btn).setOnClickListener(this);
		mBindPhoneLayout = findViewById(R.id.bind_phone_layout);
		mBindPhoneLayout.setOnClickListener(this);
		mModifyPwdLayout = findViewById(R.id.modify_pwd_layout);
		mModifyPwdLayout.setOnClickListener(this);
		mModifyDivider = findViewById(R.id.modify_pwd_divider);
		
		showUserInfo();
	}
	
	private void showUserInfo() {
		if(AppMarketApplication.isLogin()) {
			Login login = AppMarketApplication.getLogin();
			if (!TextUtils.isEmpty(login.getIconUrl())) {
				ImageLoader.getInstance().displayImage(login.getIconUrl(), mHeadIconImg, new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
						.resetViewBeforeLoading(false).build());
			}
			if (!TextUtils.isEmpty(login.getNickName())) {
				mNickNameTv.setText(login.getNickName());
			} else if (!TextUtils.isEmpty(login.getPhoneNum())) {
				String phoneNum = login.getPhoneNum();
				if (phoneNum.length() > 7) {
					mNickNameTv.setText(phoneNum.substring(0, 3) + "****" + phoneNum.substring(7));
				} else {
					mNickNameTv.setText(phoneNum);
				}
			} else {
				mNickNameTv.setText("user" + login.getUserId());
			}
			if (!TextUtils.isEmpty(login.getPhoneNum())) {
				String phoneNum = login.getPhoneNum();
				if (phoneNum.length() > 7) {
					mPhomeNumTv.setText(phoneNum.substring(0, 3) + "****" + phoneNum.substring(7));
				} else {
					mPhomeNumTv.setText(phoneNum);
				}
				mPhomeNumTv.setTextColor(mNickNameTv.getTextColors());
				mBindHintTv.setText(getResources().getString(R.string.bind));
				mBindPhoneLayout.setEnabled(false);
				mModifyPwdLayout.setVisibility(View.VISIBLE);
				mModifyDivider.setVisibility(View.VISIBLE);
			} else {
				mPhomeNumTv.setText(getResources().getString(R.string.unbind));
				mPhomeNumTv.setTextColor(Color.parseColor("#00adff"));
				mBindHintTv.setText("");
				mBindPhoneLayout.setEnabled(true);
				mModifyPwdLayout.setVisibility(View.GONE);
				mModifyDivider.setVisibility(View.GONE);
			}
		} else {
			mPhomeNumTv.setText(getResources().getString(R.string.unbind));
			mPhomeNumTv.setTextColor(Color.parseColor("#00adff"));
			mBindHintTv.setText("");
			mBindPhoneLayout.setEnabled(true);
			mModifyPwdLayout.setVisibility(View.GONE);
			mModifyDivider.setVisibility(View.GONE);
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.head_layout:
			startModifyUserInfoActivity();
			break;
		case R.id.bind_phone_layout:
			Intent intent = new Intent(this, BindPhoneActivity.class);
			startActivityForResult(intent, REQUEST_BIND_PHONE_INFO_CODE);
			break;
		case R.id.modify_pwd_layout:
			startActivity(new Intent(this, ModifyPasswdActivity.class));
			break;
		case R.id.logout_btn:
			logout();
			break;
		default:
			break;
		}
		
	}
	
	private void startModifyUserInfoActivity() {
		Intent intent = new Intent(this, ModifyUserInfoActivity.class);
		Bundle bundle = new Bundle();
		bundle.putString(ModifyUserInfoActivity.EXTRA_NAME, "完善个人资料");
		bundle.putBoolean(ModifyUserInfoActivity.EXTRA_IS_SHOW_MENU, false);
		intent.putExtras(bundle);
		startActivityForResult(intent, REQUEST_MODIFY_USER_INFO_CODE);
	}
	
	private void logout(){
		LoginUtil.logout(this);
		AppMarketApplication.setLogin(null);
		finish();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQUEST_MODIFY_USER_INFO_CODE || requestCode == REQUEST_BIND_PHONE_INFO_CODE) {
			showUserInfo();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
